package com.homegoods.resource.dialog;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.homegoods.resource.R;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.AbstractRefreshPresenter;
import com.kedll.library.mvp.RefreshingView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;


/**
 * @author QiQi
 * @ClassName: BaseListBottomSheetDialog
 * @date 2018/10/15
 * @description bottomSheet风格的列表dialogFragment
 */
public abstract class BaseListBottomSheetDialog<T extends AbstractRefreshPresenter> extends BaseBottomSheetDialogFragment implements SwipeRefreshLayout.OnRefreshListener, SwipeMenuRecyclerView.LoadMoreListener, RefreshingView {
    protected SwipeRefreshLayout mRefreshLayout;
    protected SwipeMenuRecyclerView mList;
    protected Toolbar mToolbar;
    protected TextView mTitleView;
    protected T mPresenter;
    protected MultiTypeAdapter adapter;

    protected int layoutResId() {
        return R.layout.general_include_actionbar_and_list;
    }

    protected void initView(View view) {
        mToolbar = view.findViewById(R.id.toolbar);
        mList = view.findViewById(android.R.id.list);
        mTitleView = view.findViewById(R.id.title);
        mRefreshLayout = view.findViewById(R.id.refreshLayout);

        if (mRefreshLayout != null) {
            mRefreshLayout.setColorSchemeResources(R.color.orangeRed, R.color.green);
            mRefreshLayout.setOnRefreshListener(this);
        }

        mToolbar.inflateMenu(R.menu.dialog_menu);


        mToolbar.setOnMenuItemClickListener(item -> {
            dismiss();
            return true;
        });

        mList.setLoadMoreListener(this);

        mPresenter = getPresenter();
        mList.setLayoutManager(getLayoutManager());
        mList.setHasFixedSize(true);
        adapter = mPresenter.getAdapter();
        mList.setAdapter(adapter);

    }

    @Override
    protected void loadData(Bundle arguments) {
        mList.loadMoreFinish(false, true);
        mPresenter.refreshData();
    }


    public abstract T getPresenter();

    public LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mList.clearOnScrollListeners();
        mPresenter.onDetach();
        mPresenter = null;
        if (mRefreshLayout != null) {
            mRefreshLayout.setOnRefreshListener(null);
        }
    }

    public void setTitle(String title) {
        mTitleView.setText(title);
    }

    @Override
    public void onRefresh() {
        mPresenter.refreshData();
    }

    @Override
    public void onLoadMore() {
        mPresenter.loadMoreData();
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        if (mRefreshLayout != null) {
            mRefreshLayout.setRefreshing(refreshing);
        }
    }

    @Override
    public void loadMoreFinish(boolean emptyData, boolean hasMore) {
        mList.loadMoreFinish(emptyData, hasMore);
    }

    @Override
    public void showLoad(String msg) {

    }

    @Override
    public void hideLoad() {

    }
}
